package cn.easy.kitchen.data;

import cn.easy.base.utils.ExtractUtil;
import cn.easy.base.utils.FreeMarkers;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.Transient;

/**
 * Created by chenzhongyi on 15/10/8.
 */

public abstract class DatabaseMeta {
    public static final int DB_TYPE_MYSQL = 1;
    public static final int DB_TYPE_ORACLE = 2;
    public static final int DB_TYPE_SQLSERVER = 3;

    protected Integer type = DB_TYPE_ORACLE;
    protected String hostName;
    protected String dbName;
    protected String dbPort;
    protected String sid;
    protected String username;
    protected String password;

    public DatabaseMeta() {

    }

    @Transient
    @JsonIgnore
    public String getDriverClassName() {
        if (type == DB_TYPE_MYSQL) {
            return "com.mysql.jdbc.Driver";
        } else if (type == DB_TYPE_ORACLE) {
            return "oracle.jdbc.driver.OracleDriver";
        } else if (type == DB_TYPE_SQLSERVER) {
            return "com.microsoft.sqlserver.jdbc.SQLServerDriver";
        }
        return "";
    }

    @Transient
    @JsonIgnore
    public String getUrl() {
        if(type == null || StringUtils.isBlank(hostName) || StringUtils.isBlank(dbPort)) {
            return null;
        }
        String urlTemplate = "";
        if (type == DB_TYPE_MYSQL) {
            urlTemplate = "jdbc:mysql://${hostName}:${dbPort}/${dbName}?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&failOverReadOnly=false&connectTimeout=3000&socketTimeout=60000";
        } else if (type == DB_TYPE_ORACLE) {
            urlTemplate = "jdbc:oracle:thin:@${hostName}:${dbPort}:${sid}";
        } else if (type == DB_TYPE_SQLSERVER) {
            urlTemplate = "jdbc:sqlserver://${hostName}:${dbPort};DatabaseName=${dbName}";
        }
        return FreeMarkers.renderString(urlTemplate, ExtractUtil.object2Map(this));
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public String getHostName() {
        return hostName;
    }

    public void setHostName(String hostName) {
        this.hostName = hostName;
    }

    public String getDbName() {
        return dbName;
    }

    public void setDbName(String dbName) {
        this.dbName = dbName;
    }

    public String getDbPort() {
        if (dbPort == null) {
            if (type == DB_TYPE_MYSQL) {
                return "3306";
            } else if (type == DB_TYPE_ORACLE) {
                return "1521";
            } else if (type == DB_TYPE_SQLSERVER) {
                return "1433";
            }
        }
        return dbPort;
    }

    public void setDbPort(String dbPort) {
        this.dbPort = dbPort;
    }

    public String getSid() {
        return sid;
    }

    public void setSid(String sid) {
        this.sid = sid;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }


}
